<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="用户名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="用户账号">
      </el-table-column>
      <el-table-column
        prop="ip"
        header-align="center"
        align="center"
        label="登录IP地址">
      </el-table-column>
      <el-table-column
        prop="address"
        header-align="center"
        align="center"
        label="登录地点">
      </el-table-column>
      <el-table-column
        prop="browser"
        header-align="center"
        align="center"
        label="浏览器类型">
      </el-table-column>
      <el-table-column
        prop="os"
        header-align="center"
        align="center"
        label="操作系统">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="登录状态（成功/ 失败）">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status.indexOf('成功') > -1" size="small" type="success">{{scope.row.status}}</el-tag>
          <el-tag v-if="scope.row.status.indexOf('失败') > -1" style="cursor: pointer;" @click="openDetails(scope.row.id)" size="small" type="danger">{{scope.row.status}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="loginTime"
        header-align="center"
        align="center"
        label="访问时间">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 异常详情 -->
    <log-details v-if="detailsVisible" ref="logDetails" />
  </div>
</template>

<script>
  import LogDetails from './log-details'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        detailsVisible: false
      }
    },
    components: {
      LogDetails
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl(`/sys/loginlog/page/list/${this.pageIndex}/${this.pageSize}`),
          method: 'get',
          params: this.$http.adornParams({
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.success) {
            this.dataList = data.queryResult.list
            this.totalPage = data.queryResult.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      openDetails (id) {
        this.detailsVisible = true
        this.$nextTick(() => {
          this.$refs.logDetails.init(id, 1)
        })
      }
    }
  }
</script>
